

import React from 'react';
import { Loading } from './loading';

/**
 * 懒加载组件
 * @param factory 组件工厂函数
 * @param load 加载时的组件
 */
export const lazy = (factory: () => Promise<any>, load = <Loading />) => (props: any) => {
    const [Ct, setCt] = React.useState<any>();
    React.useEffect(() => {
        (async () => {
            const myCt = await factory();
            setCt(myCt);
        })();
    }, []);
    return (Ct && Ct.default) ? <Ct.default {...props} /> : load;
};

